<?php 
Class articleController extends appController
{
	
	public function index()
	{
		
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$sort['cat_id']=intval($_GET['c']);
		$sort['keyword']=isset($_GET['k'])?trim($_GET['k']):'';
		
		$art_list=Model('article')->art_list($page,$sort);
		$this->assign('cat_list',Model('menu')->get_parent_menu());
		$this->assign('art_list',$art_list['data']);
		$this->assign('art_page',$art_list['pagelink']);
		
		$this->assign('cat_id',$cat_id);
		$this->display('index.html');
		
	}
	
	public function art_html()
	{
		$id=$_REQUEST['id'];
		$art=Model('article')->get_html_art($id);
		if($art)
		{
	
		$saveDir=HTML_PATH.'/'.$art['code'];
		$activePage="http://".$_SERVER['HTTP_HOST']."/index.php?g=home&m=article&a=view&id=$id";
		
		$fileName=$art['addtime'].$art['id'];
		
		$this->create_html($activePage,$fileName,$saveDir);
		}
	}
	public function ordering()
	{
		$id=isset($_POST['id'])?intval($_POST['id']):'';
		$val=isset($_POST['val'])?intval($_POST['val']):'';
		$result=Model('article')->ordering($val,$id);
		print_r(json_encode($result));
	}
	public function del()
	{
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		$result=array('error'=>1,'content'=>'删除失败');
		$res=Model('article')->del(array('table'=>'article','id'=>$id));
		$res2=Model('article')->del(array('table'=>'album_photos','aid'=>$id));
		 if($res&&$res2)
		 {
		 	$result['error']=0;
		 	$result['content']='删除成功';
		 }
		print_r(json_encode($result));
	
	}
	public function page()
	{
	
		$this->assign('forms_list',Model('forms')->get_all_forms());
		$this->assign('parent_list',Model('menu')->get_parent_menu());
		$cat_id=isset($_GET['c'])?intval($_GET['c']):'';
		$id=Model('article')->get_page_art($cat_id);//查找与该分类相关的文章
		if($id)
		{
			$art_info=Model('article')->get_art($id);
			$this->assign('id',$id);
			$this->assign('art',$art_info);
		  $this->assign('action','modified_art');
		}
		else 
		{
			$art_info['cat_id']=$cat_id;
			$this->assign('art',$art_info);
			$this->assign('action','new_art');
		}
	
		$this->display('article.html');
	
	}
	public function edit_art()
	{
		
		$this->assign('forms_list',Model('forms')->get_all_forms());
		$this->assign('parent_list',Model('menu')->get_parent_menu());
		$id=isset($_GET['id'])?intval($_GET['id']):'';
		$this->assign('id',$id);
		$this->assign('ur_here','>编辑文章');
		$art_info=Model('article')->get_art($id);
		$this->assign('art',$art_info);
		$this->assign('action','modified_art');
		
		$this->display('article.html');
	
	}
	public function update_photo()
	{
		$en_abstract=isset($_POST['en_abstract'])?$_POST['en_abstract']:'';
		$en_title=isset($_POST['en_title'])?$_POST['en_title']:'';
		$info['title']=isset($_POST['title'])?CH_SC.$_POST['title'].DIG_EC.EN_SC.$en_title.DIG_EC:'';
		$info['abstract']=isset($_POST['abstract'])?CH_SC.$_POST['abstract'].DIG_EC.EN_SC.$en_abstract.DIG_EC:'';
		$id=isset($_POST['id'])?intval($_POST['id']):'';
		$data=array('error'=>0,'content'=>'','url'=>'');
		if(Model('article')->update_photo($info,$id))
		{
			$data['content']=Lang('SUCCESS_EDIT');
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
			//$data['url']=$_SERVER['REQUEST_URI'];
		}
		$this->showMsg($data);
	
	}
	public function edit_photo()
	{
		$id=isset($_REQUEST['pid'])?$_REQUEST['pid']:'';
		$photo=Model('article')->get_photo($id);
		$this->assign('photo',$photo);
		$plist=Model('article')->photo_list($id);
		$index=0;
		if($plist)
		{
			foreach($plist as $k=>$v)
			{
				if($v['id']==$id)
				{
					$index=$k;
				}
			}
		}
		$this->assign('index',$index);
		$this->assign('plist',$plist);
		$this->display('photo_list.html');
		
	}
	public function get_photo()
	{
		$id=isset($_REQUEST['pid'])?$_REQUEST['pid']:'';
		$result=array('error'=>1,'content'=>'','data'=>'');
		$plist=Model('article')->get_photo($id);
		if($plist)
		{
			$result['error']=0;
			$result['data']=$plist;
		}
		print_r(json_encode($result));
	}
	public function del_photo()
	{
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('article')->del_photo($v);
			}
		}
		else
		{
			$result=Model('article')->del_photo($id);
		}
		print_r(json_encode($result));
	}
	public function modified_art()
	{
		$id=isset($_POST['id'])?$_POST['id']:'';
		$info['cat_id']=isset($_POST['cat_id'])?$_POST['cat_id']:'';
		$en_summary=isset($_POST['en_summary'])?$_POST['en_summary']:'';
		$info['flags']=is_array($_POST['flags'])?implode(',', $_POST['flags']):$_POST['flags'];
		$info['summary']=isset($_POST['summary'])?CH_SC.$_POST['summary'].DIG_EC.EN_SC.$en_summary.DIG_EC:'';
		$en_title=isset($_POST['en_title'])?$_POST['en_title']:'';
		$en_author=isset($_POST['en_author'])?$_POST['en_author']:'';
		$en_content=isset($_POST['en_content'])?$_POST['en_content']:'';
		$info['title']=isset($_POST['title'])?CH_SC.$_POST['title'].DIG_EC.EN_SC.$en_title.DIG_EC:'';
		$info['abstract']=isset($_POST['abstract'])?$_POST['abstract']:'';
		$info['keywords']=isset($_POST['keywords'])?$_POST['keywords']:'';
		$info['author']=isset($_POST['author'])?CH_SC.$_POST['author'].DIG_EC.EN_SC.$en_author.DIG_EC:'';
		$info['source']=isset($_POST['source'])?$_POST['source']:'';
		$info['content']=isset($_POST['content'])?CH_SC.$_POST['content'].DIG_EC.EN_SC.$en_content.DIG_EC:'';
		$info['ordering']=isset($_POST['ordering'])?$_POST['ordering']:'';
		$info['hits']=isset($_POST['hits'])?$_POST['hits']:'';
		$info['source_url']=isset($_POST['source_url'])?$_POST['source_url']:'';
		$info['page_title']=isset($_POST['page_title'])?$_POST['page_title']:'';
		$info['created']=$_POST['created']==''?$_SERVER['REQUEST_TIME']:strtotime($_POST['created']);
		$info['page_title']=isset($_POST['page_title'])?$_POST['page_title']:'';
		$info['thumb']=isset($_POST['thumb'])?$_POST['thumb']:'';
		$info['form_id']=isset($_POST['form_id'])?$_POST['form_id']:0;
		
		$photo=isset($_POST['photo'])?$_POST['photo']:array();
		
		if($_POST['title']=='')
		{
			$data['error']=1;
			$data['content']=Lang('标题不能为空');
			$this->showMsg($data);
		}
		
		$result=Model('article')->update_art($info,$photo,$id);
		
		$data=array('error'=>0,'content'=>'');
		if($result)
		{
			$data['content']=Lang('SUCCESS_EDIT');
			$data['error']=0;
			
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
			//$data['url']=$_SERVER['REQUEST_URI'];
		}
		print_r(json_encode($data));
		 exit();
	}
     public function add_art()
	{
		$this->assign('forms_list',Model('forms')->get_all_forms());
		$this->assign('parent_list',Model('menu')->get_parent_menu());
		$this->assign('ur_here','>添加文章');
		$cat_id=isset($_GET['cat_id'])?intval($_GET['cat_id']):'';
		$this->assign('cat_id',$cat_id);
		$this->assign('action','new_art');
		$this->display('article.html');
	
	}
	
	public function new_art()
	{
		$data=array('error'=>1,'content'=>'');
		
		$info['cat_id']=isset($_POST['cat_id'])?$_POST['cat_id']:'';
		//自动截取summary
		if($_POST['summary']===''&&$_POST['content']!='')
		{
			$_POST['summary']= hy_substr(str_replace(PHP_EOL, '', strip_tags($_POST['content'])),200);
		}
		if($_POST['en_summary']===''&&$_POST['en_content']!='')
		{
			$_POST['en_summary']= hy_substr(str_replace(PHP_EOL, '', strip_tags($_POST['en_content'])),200);
		}
		$en_summary=isset($_POST['en_summary'])?$_POST['en_summary']:'';
		$info['summary']=isset($_POST['summary'])?CH_SC.$_POST['summary'].DIG_EC.EN_SC.$en_summary.DIG_EC:'';
		$en_title=isset($_POST['en_title'])?$_POST['en_title']:'';
		$en_author=isset($_POST['en_author'])?$_POST['en_author']:'';
		$en_content=isset($_POST['en_content'])?$_POST['en_content']:'';
		$info['title']=isset($_POST['title'])?CH_SC.$_POST['title'].DIG_EC.EN_SC.$en_title.DIG_EC:'';
		$info['abstract']=isset($_POST['abstract'])?$_POST['abstract']:'';
		$info['flags']=is_array($_POST['flags'])?implode(',', $_POST['flags']):$_POST['flags'];
		$info['keywords']=isset($_POST['keywords'])?$_POST['keywords']:'';
		$info['author']=isset($_POST['author'])?CH_SC.$_POST['author'].DIG_EC.EN_SC.$en_author.DIG_EC:'';
		$info['source']=isset($_POST['source'])?$_POST['source']:'';
		$info['content']=isset($_POST['content'])?CH_SC.$_POST['content'].DIG_EC.EN_SC.$en_content.DIG_EC:'';
		$info['ordering']=isset($_POST['ordering'])?$_POST['ordering']:'';
		$info['hits']=isset($_POST['hits'])?$_POST['hits']:0;
		$info['source_url']=isset($_POST['source_url'])?$_POST['source_url']:'';
		$info['page_title']=isset($_POST['page_title'])?$_POST['page_title']:'';
		$info['created']=$_POST['created']==''?$_SERVER['REQUEST_TIME']:strtotime($_POST['created']);
		$info['page_title']=isset($_POST['page_title'])?$_POST['page_title']:'';
		$info['thumb']=isset($_POST['thumb'])?$_POST['thumb']:'';
		$info['is_show']=isset($_POST['is_show'])?$_POST['is_show']:1;
		$photo=is_array($_POST['photo'])?$_POST['photo']:array($_POST['photo']);
		$info['form_id']=isset($_POST['form_id'])?$_POST['form_id']:0;
		if($_POST['title']==='')
		{
			
			$data['content']=Lang('KEY_EMPTY');
			print_r(json_encode($data));
		    exit();
		}
		if(Model('article')->create_art($info,$photo))
		{
			$data['content']=Lang('SUCCESS_ADD');
			$data['error']=0;
			
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
			//$data['url']=$_SERVER['REQUEST_URI'];
		}
		print_r(json_encode($data));
		 exit();
	}
	
	
	

	
	
}